package concurent

import (
	"fmt"
	"runtime"
	"sync"
	"time"
)

// 类似java中的信号量概念
var wg sync.WaitGroup

func hello(i int) {
	// goroutines结束就登记-1
	defer wg.Done()
	fmt.Println("hello sync wg", i)
}

func Go_wg_1() {
	for i := 0; i < 5; i++ {
		// 启动一个goroutine就等级+1
		wg.Add(1)
		go hello(i)
	}
	// 等到所有等级的goroutines都结束
	wg.Wait()
	fmt.Println("================")
}

func GoMaxProcs() {
	fmt.Printf("runtime.NumCPU(): %v\n", runtime.NumCPU())
	// 逻辑CPU数量设置 默认最大的 8  设置1就会顺序执行
	//runtime.GOMAXPROCS(1)
	go printA10Times()
	go printB10Times()
	time.Sleep(time.Second)
}

func printA10Times() {
	for i := 0; i < 10; i++ {
		fmt.Println("A:", i)
	}
}

func printB10Times() {
	for i := 0; i < 10; i++ {
		fmt.Println("B:", i)
	}
}
